package com.hznu.jspFinal.controller;

import com.hznu.jspFinal.entity.Kpoint;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.KpointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * author :wp
 * time : 10:22 2018/06/07
 */
@Api(description = "知识点管理接口" ,value="Kpoint")
@RestController
@RequestMapping("/Kpoint")
public class KpointController {
    @Autowired
    private KpointService kpointService;

    @ApiOperation(value = "新增知识点")
    @PostMapping("/add")
    public R addOne(@RequestBody Kpoint kpoint)
    {
        try{
            kpointService.addOne(kpoint);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "删除知识点")
    @GetMapping("/delete/{id}")
    public R delete(@PathVariable Integer id)
    {
        try{
            kpointService.delete(id);
            return R.ok();
        }catch (Exception e){
            return R.error();
        }
    }

    @ApiOperation(value = "修改知识点")
    @PostMapping("/update")
    public R delete(@RequestBody Kpoint kpoint)
    {
        try{
            kpointService.update(kpoint);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "通过id查找知识点")
    @GetMapping("/findById/{id}")
    public R findById(@PathVariable Integer id)
    {
        try{
            return R.ok().put("data",kpointService.findById(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查找courseid对应的知识点列表")
    @GetMapping("/findAllByCourseid/{courseid}")
    public R findAllByCourseid(@PathVariable Integer courseid)
    {
        try{
            return R.ok().put("data",kpointService.findByCourseid(courseid));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}